﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PrototypeZGame
{
    public class ZombieAI : AIController
    {
        private enum State
        {
            Wandering,
            Hunting,
            Dead,
        }

        State state;

        public ZombieAI(World world)
            : base(world)
        {
            state = State.Wandering;
        }

        private Actor findNewTarget()
        {
            float minDistance = float.MaxValue;
            Human closestHuman = null;
            foreach (Human human in world.humans)
            {

                if (!human.dead && World.Distance(unit, human) < minDistance)
                {
                    minDistance = World.Distance(unit, human);
                    closestHuman = human;
                }
            }
            if (minDistance <= unit.viewDistance)
            {
                return closestHuman;
            }
            else
            {
                return null;
            }
        }

        public override void newTurn()
        {
            unit.newTurn();

            switch (state)
            {
                case State.Wandering:
                    target = findNewTarget();
                    if (target == null)
                    {
                        int r = world.rand.Next(4);

                        if (r == 0)
                            walk(Direction.North);
                        if (r == 1)
                            walk(Direction.South);
                        if (r == 2)
                            walk(Direction.West);
                        if (r == 3)
                            walk(Direction.East);
                        break;
                    }
                    else
                    {
                        state = State.Hunting;
                        break;
                    }                    
                case State.Hunting:
                    if ((target as Unit).dead)
                    {
                        target = null;
                        state = State.Wandering;
                        break;
                    }


                    if (World.Distance(unit, target) <= unit.handsReach)
                    {
                        unit.Attack((Unit)target);
                        break;
                    }
                    
                    Result result = MoveToTarget((int)unit.viewDistance + 2);
                    if (result == Result.NoPath)
                    {
                        target = null;
                        state = State.Wandering;
                    }
                    else if (result == Result.BlockingDoor)
                    {
                        breakDoor();
                    }
                    

                    

                    break;
                case State.Dead:
                    break;
            }

        }

        private void breakDoor()
        {
            Vec2 targetPosition = unit.position + World.DirToVec2(path[0]);
            Field targetField = terrain.getField(targetPosition);
            List<Item> items = targetField.onFieldItems;
            foreach (Item item in items)
            {
                Door door = item as Door;
                if (door != null)
                {
                    unit.Attack(door);
                    break;
                }
            }
        }

        public override void UnitDied()
        {
            state = State.Dead;
        }
    }
}
